<?php

namespace common\models\modelled;

use Yii;
use yii\behaviors\TimestampBehavior;
use common\models\modelled\Modeloption;
/**
 * This is the model class for table "modelled".
 *
 * @property integer $id
 * @property string $category
 * @property string $name
 * @property integer $created_at
 * @property integer $updated_at
 * @property string $option
 */
class Modelled extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'modelled';
    }


    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['created_at', 'updated_at'], 'integer'],
            [['category', 'name', 'option'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'category' => Yii::t('app', '分类'),
            'name' => Yii::t('app', 'Name'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
            'option' => Yii::t('app', '字段类型'),
        ];
    }
    public static function createform($type,$name,$keyname,$keyvalue=""){
        switch ($type) {
            case '简单输入框':
                 $trueval ='<div class="form-group field-inputtext">
                        <label class="control-label" for="inputtext">'.$name.'</label>
                        <input type="text" id="inputtext" value="'.$keyvalue.'" class="form-control" name="'.$keyname.'" maxlength="255">

                        <div class="help-block"></div>
                    </div>';
                break;
            case '长文本输入框':
                $trueval = '<div class="form-group field-intro has-success">
                    <label class="control-label" for="intro">'.$name.'</label>
                    <textarea id="intro" class="form-control" name="'.$keyname.'" rows="6">'.$keyvalue.'</textarea>

                    <div class="help-block"></div>
                </div>';
                break;
            case '下拉框':
                
                $modeled = Modeloption::find()->andWhere(['type'=>$name])->asArray()->all();
                $arr = '';
            
                foreach($modeled as $key=>$val){
                    $sel = '';
                    if($val['name']==$keyvalue){
                        $sel = 'selected = "selected"';
                    }
                    $arr .= '<option '.$sel.' value="'.$val["name"].'">'.$val["name"].'</option>';
                }
                $trueval = '
                <div class="form-group field ">
                    <label class="control-label" >'.$name.'</label>
                    <select class="form-control" name="'.$keyname.'">
                    '.$arr.'
                    </select>

                    <div class="help-block"></div>
                </div>
                ';
                break;
            case '单选框':
 
                $modeled = Modeloption::find()->andWhere(['type'=>$name])->asArray()->all();
                $arr = '';
                
                foreach($modeled as $key=>$val){
                    $sel = '';
                    if($val['name']==$keyvalue){
                        $sel = 'checked="checked" ';
                    }    
                    $arr .= '<label><input type="radio" '.$sel.' name="'.$keyname.'" value="'.$val["name"].'">'.$val["name"].'</label>';
                }
                $trueval = '
                    <div class="form-group field ">
                        <label class="control-label">'.$name.'</label>
                        <div >
                        '.$arr.'
                        </div>

                        <div class="help-block"></div>
                    </div>
                ';
                break;
            case '多选':
           
                $modeled = Modeloption::find()->andWhere(['type'=>$name])->asArray()->all();
                $arr = '';
            
                foreach($modeled as $key=>$val){
                     $sel = '';
                     if(!empty($keyvalue)){
                        if(in_array($val['name'],$keyvalue)){
                             $sel = 'checked="checked" ';
                        }
                     }
          
                    $arr .= '<label><input type="checkbox" '.$sel.' name="'.$keyname.'[]" value="'.$val["name"].'">'.$val["name"].'</label>';
                }
                $trueval = '
                <div class="form-group field-member-phone required">
                    <label class="control-label">'.$name.'</label>
                    <div>
                    '.$arr.'
                    </div>

                    <div class="help-block"></div>
                </div>
                ';
                break;
            default:
                # code...
                break;
        }
        return $trueval;
    }
}
